package org.jeecg.modules.demo.cms.web_controller;

import cn.hutool.core.text.split.SplitIter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.select.Join;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.SelectTreeModel;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.cms.article.entity.CmsArticle;
import org.jeecg.modules.demo.cms.article.service.ICmsArticleService;
import org.jeecg.modules.demo.cms.article.vo.CmsArticleVo;
import org.jeecg.modules.demo.cms.article_content.service.ICmsArticleContentService;
import org.jeecg.modules.demo.cms.column_type.entity.CmsColumnType;
import org.jeecg.modules.demo.cms.column_type.service.ICmsColumnTypeService;
import org.jeecg.modules.demo.cms.web_controller.vo.JkExpertVo;
import org.jeecg.modules.demo.domain.types.entity.CmsDomainType;
import org.jeecg.modules.demo.domain.types.service.ICmsDomainTypeService;
import org.jeecg.modules.demo.expert.info.entity.JkExpert;
import org.jeecg.modules.demo.expert.info.entity.JkExpertWork;
import org.jeecg.modules.demo.expert.info.service.IJkExpertService;
import org.jeecg.modules.demo.expert.info.vo.JkExpertPage;
import org.jeecg.modules.demo.expert.info.vo.JkExpertPageVo;
import org.jeecg.modules.demo.lunbo.lunbo_dict.entity.CmsLunboPosition;
import org.jeecg.modules.demo.lunbo.lunbo_dict.service.ICmsLunboPositionService;
import org.jeecg.modules.demo.lunbo.lunbo_info.entity.CmsLunboBannter;
import org.jeecg.modules.demo.lunbo.lunbo_info.service.ICmsLunboBannterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.PrivateKey;
import java.util.*;

/**
* @Description: 网站新闻表
* @Author: jeecg-boot
* @Date:   2025-05-23
* @Version: V1.0
*/
@Tag(name="网站新闻表")
@RestController
//@RequestMapping("/api/cms/article/cmsArticle")
@RequestMapping("/api/cms")
@Slf4j
public class WebCmsArticleController extends JeecgController<CmsArticle, ICmsArticleService> {
   @Autowired
   private ICmsArticleService cmsArticleService;
   @Autowired
   private ICmsArticleContentService cmsArticleContentService;
   @Autowired
   private ICmsColumnTypeService cmsColumnTypeService;
   @Autowired
   private ICmsDomainTypeService cmsDomainTypeService;
   @Autowired
   private ICmsLunboPositionService cmsLunboPositionService;
   @Autowired
   private ICmsLunboBannterService cmsLunboBannterService;
   @Autowired
   private IJkExpertService jkExpertService;

    /**
     * 分页列表查询 返回 内容属性
     *
     * @param cmsArticle
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "网站新闻表-分页列表查询")
    @Operation(summary="网站新闻表-分页列表查询")
    @GetMapping(value = "/article_list")
    public Result<IPage<CmsArticle>> article_list(CmsArticle cmsArticle,
                                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                   HttpServletRequest req) {
        //发布的新闻
        QueryWrapper<CmsArticle> queryWrapper = QueryGenerator.initQueryWrapper(cmsArticle, req.getParameterMap());

        queryWrapper.eq("zhuangt","5");
        Page<CmsArticle> page = new Page<CmsArticle>(pageNo, pageSize);
        IPage<CmsArticle> pageList = cmsArticleService.page(page,queryWrapper);
        return Result.OK(pageList);
    }
    /**
     * 新闻模块列表（栏目分类）的查询,若传pid返回一级栏目，若不传返回二级栏目
     */
    @Operation(summary="新闻模块分类（栏目分类）的查询")
    @GetMapping(value = "/select_column_type_by_pid")
    public Result<List<CmsColumnType>> select_column_type_by_pid( @RequestParam(name = "pid", required = false) String pid) {

        if (oConvertUtils.isEmpty(pid)) {
            pid = "0";
        }
        QueryWrapper<CmsColumnType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        List<CmsColumnType> list = cmsColumnTypeService.list(queryWrapper);
//        List<CmsColumnType> list = cmsColumnTypeService.select_column_type_by_pid(pid);
        return Result.OK(list);

    }
    /**
     * 领域分类等级的查询，若传pid返回一级领域等级，若不传返回二级领域等级
     */
    @Operation(summary="领域分类等级的查询")
    @GetMapping(value = "/select_domain_type_by_pid")
    public Result<List<CmsDomainType>> select_domain_type_by_pid( @RequestParam(name = "pid", required = false) String pid) {

//        String parentId = cmsDomainType.getPid();
        if (oConvertUtils.isEmpty(pid)) {
            pid = "0";
        }
        QueryWrapper<CmsDomainType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",pid);
        List<CmsDomainType> domainTypes = cmsDomainTypeService.list(queryWrapper);
        return Result.OK(domainTypes);

    }
    /**
     * 轮播图位置查看，若传pid返回一级，若不传返回二级
     */
    @Operation(summary="轮播图位置查询/未启用")
    @GetMapping(value = "/select_lunbo_position_by_pid")
    public Result<List<CmsLunboPosition>> select_lunbo_position_by_pid(@RequestParam(name = "pid", required = false) String pid) {
//        String parentId = cmsDomainType.getPid();
        if (oConvertUtils.isEmpty(pid)) {
            pid = "0";
        }
        QueryWrapper<CmsLunboPosition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",pid);
        List<CmsLunboPosition> lunboPositions = cmsLunboPositionService.list(queryWrapper);
        return Result.OK(lunboPositions);
    }
    /**
     * 轮播图位置查看，若传pid返回一级，若不传返回二级
     */
    @Operation(summary="根据位置查看轮播图")
    @GetMapping(value = "/select_lunbo_image_by_position")
    public Result<List<CmsLunboBannter>> select_lunbo_image_by_position(@RequestParam(name = "positionCode", required = false) String positionCode) {

//        根据位置编码查询位置id
            QueryWrapper<CmsLunboPosition> queryPositonWrapper = new QueryWrapper<>();
            queryPositonWrapper.eq("position_code",positionCode);
            CmsLunboPosition position = cmsLunboPositionService.getOne(queryPositonWrapper);
            //根据位置id查询轮播图
            QueryWrapper<CmsLunboBannter> queryWrapper = new QueryWrapper<>();
            String position_id = position.getId();
            queryWrapper.eq("position_id",position_id);
            //是否轮播 Y是 N否
            queryWrapper.eq("lunbo_status","Y");
            List<CmsLunboBannter> lunboPositionList = cmsLunboBannterService.list(queryWrapper);
            return Result.OK(lunboPositionList);
    }

    /**
     * 分页列表查询  传领域分类
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "专家信息表-分页列表查询")
    @Operation(summary="专家信息表-分页列表查询")
    @GetMapping(value = "/query_jk_expert_list")
    public Result<IPage<JkExpert>> query_jk_expert_list(@RequestParam(name = "typeCpode", required = false) String typeCode,
                                                 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                 HttpServletRequest req) {
       
        // 自定义查询规则
        Map<String, QueryRuleEnum> customeRuleMap = new HashMap<>();
        // 自定义多选的查询规则为：LIKE_WITH_OR
        customeRuleMap.put("isVerified", QueryRuleEnum.LIKE_WITH_OR);
        JkExpert jkExpert = new JkExpert();
        QueryWrapper<JkExpert> queryWrapper = QueryGenerator.initQueryWrapper(jkExpert, req.getParameterMap(),customeRuleMap);
        Page<JkExpert> page = new Page<JkExpert>(pageNo, pageSize);
        if(!oConvertUtils.isEmpty(typeCode)){
            //如果传进来的code不等于空 则去查询该类型所涉及的所有专家
            IPage<JkExpert> pageLists =jkExpertService.select_expert_by_code(typeCode,page);
            return  Result.OK(pageLists);
        }
        IPage<JkExpert> pageList = jkExpertService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    @Operation(summary="专家信息表-详情")
    @GetMapping(value = "/query_jk_expert_details_by_id")
    public Result<JkExpertPageVo> query_jk_expert_details_by_id(@RequestParam("id") String id,
                                                                           @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                           @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                                           HttpServletRequest req) {

        JkExpertPageVo jkExpert = jkExpertService.query_jk_expert_details_by_id(id);


        return Result.OK(jkExpert);
    }


}
